package com.interceptor;

import com.model.FUser;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    //控制器方法前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws  Exception{
        //判断收费为需拦截的请求
        //获取Session
        HttpSession session =request.getSession();
        FUser user =(FUser) session.getAttribute("USER_SESSION");
        //判断Session中是否有用户数据，如果有，则返回true，继续向下执行
        if (user != null){
            return true;
        }

        request.setAttribute("msg","您还没有登录，请先登录！");
        request.getRequestDispatcher("login").forward(request,response);
        return false;
    }
}
